<?php

class add_edit_category extends a_admin_controller {
   
    protected $categoryTree;

    public function __construct() {
        parent::__construct();
      
        $cat_service = new service_l_category();
        $this->categoryTree = $cat_service->get_cat_tree_menu("tree");

    }
    
    public function get_catInfo_subCats()
    {
        $cat_service = new service_l_category();
        $sub_cats_json = array();
        $requested_cat_id = $_POST['cat_id'];
        $requested_category = service_l_category::get_category($requested_cat_id);
        $requested_cat_path = $cat_service->get_cat_path($requested_cat_id, "cat_path");
        $sub_cats = service_l_category::get_next_level_child_categories($requested_cat_id);
        
        $sub_cats_json[] = array("Cat_path" => $requested_cat_path);
        $sub_cats_json[] = array("ID" => $requested_category->id,
                                  "Title" =>$requested_category->get_desc()->title,
                                  "Code" =>$requested_category->code,
                                  "Short_desc" =>$requested_category->get_desc()->short_desc,
                                  "Long_desc" =>$requested_category->get_desc()->long_desc);
        foreach ($sub_cats as $sub_cat)
            $sub_cats_json[] = array(
                                      "ID" => $sub_cat->id,
                                      "Title" =>$sub_cat->get_desc()->title,
                                      "Code" =>$sub_cat->code,
                                      "Action" => "<img src='".$this->global_smarty_vars['core_images']."/common/detail.png' title='Details' style='cursor:pointer'".
                                                        "onclick='view_subCat(".$sub_cat->id.");'> &nbsp;&nbsp;&nbsp;&nbsp".
                                                   "<img src='".$this->global_smarty_vars['core_images']."/common/edit.png' title='Edit/Update' style='cursor:pointer'".
                                                        "onclick='edit_subCat(".$sub_cat->id.");'> &nbsp;&nbsp;&nbsp;&nbsp".
                                                  "<img src='".$this->global_smarty_vars['core_images']."/common/delete.png' title='Delete' style='cursor:pointer'".
                                                        "onclick='delete_subCat(".$sub_cat->id.");'>"
                                                  
                                    );

        echo json_encode($sub_cats_json);
    }

    public function get_category()
    {
         $category = service_l_category::get_category( $_POST['cat_id']);

         $sub_cats_json = array();
         $sub_cats_json[] = array("ID" => $category->id,
                                  "Title" =>$category->get_desc()->title,
                                  "Code" =>$category->code,
                                  "Short_desc" =>$category->get_desc()->short_desc,
                                  "Long_desc" =>$category->get_desc()->long_desc);
         echo json_encode($sub_cats_json);
    }

    /*
     *      This Function will be used for both Add (New) and Edit (Existing) Category
     *
     */
    public function save_category()
    {
        $cat_id = $_POST['cat_id'];
        $vars = array();
        $vars['code'] = $_POST['code'];
        ////$vars['publish'] = $_POST['publish'];
        $vars['title'] = $_POST['title'];
        $vars['short_desc'] = $_POST['short_desc'];
        $vars['long_desc'] = $_POST['long_desc'];
        ////$vars['language_id'] = $_POST['language_id'];

        $vars['parent_cat_id'] = $_POST['parent_cat_id'];
        $vars['cat_id'] = $cat_id;
        $category = service_l_category::save_category($vars);

        $sub_cats_json = array();
        if($cat_id == "null")   // For new Category
            $sub_cats_json[] = array("Title" => $category->get_desc()->title,
                                     "Key" => $category->id
                                    );
        else    // Edit existing Category
            $sub_cats_json[] = array("result" => "Category Edited successfully!!");
        
        echo json_encode($sub_cats_json);
    }
    public function delete_category()
    {
        $cat_id = $_POST['cat_id'];
        service_l_category::delete_category($cat_id);
        $sub_cats_json = array();
        $sub_cats_json[] = array("result" => "Successfully deleted Category, ID = ".$cat_id);
        echo json_encode($sub_cats_json);
    }
    public function index()
    {
        //echo $this->categoryTree;
    }

    public function batch_assign(){
       $this->assign("tree", $this->categoryTree);
    }


}
?>